package educar.gui;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Hashtable;
import javax.swing.JList;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JMenuItem;

import educar.controllers.IController;
import educar.utils.Utilidades;

public class GenericListener implements MouseListener
{
    private Hashtable<JComponent, IController> componentControllerMap;

    public GenericListener()
    {
        componentControllerMap = new Hashtable<JComponent, IController>();
    }

    public void associate(JComponent c, IController controller)
    {
        c.addMouseListener(this);
        componentControllerMap.put(c, controller);
    }

    public void mouseClicked(MouseEvent e)
    {
        IController controller = componentControllerMap.get(e.getSource());
        Object component = e.getSource();
        if (component  instanceof JList)
        {
            JList lista = (JList) component;
            if (!lista.isSelectionEmpty())
            {
                String it = (String)lista.getSelectedValue();
                String name = Utilidades.getSelectedID(it);
                String item = lista.getName();
                controller.processItemList(name, item);
            }
        }
        else if (component instanceof JButton)
        {
            JButton b = (JButton) component;
            controller.process(b.getActionCommand());
        }
    }

    public void mouseEntered(MouseEvent e)
    {
    	
    }

    public void mouseExited(MouseEvent e)
    {
          
    }

    public void mousePressed(MouseEvent e)
    {
    	   IController controller = componentControllerMap.get(e.getSource());
           Object component = e.getSource();
           if (component instanceof JMenuItem)
           {
               JMenuItem m = (JMenuItem) component;
               controller.process(m.getActionCommand());
           }       
    }

    public void mouseReleased(MouseEvent e)
    {
    	  
    }
}
